package com.xca.znb.database;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DBHelper extends SQLiteOpenHelper {

   private static final String TAG = "DBHelper";
   private static final String DATABASE_NAME = "znb.db";// 数据库名
   private static final int DATABASE_VERSION = 1;// 数据库版本号
   private static final String TABLE_NAME_GOODS = "goods"; //商品表
   private static final String TABLE_NAME_USER = "user"; //用户表
   private static final String TABLE_NAME_ORDER = "order2"; // 订单表 order是关键字
   private static final String TABLE_NAME_SETTINGS = "settings"; // 设置表
   private static final String TABLE_NAME_PHOTO = "photo";//照片表
   private static final String TABLE_NAME_SHOPPING_CART = "shopping_cart";//购物车
   private static final String TABLE_NAME_FOOTPRINT = "footprint" ;//足迹表
   private static final String TABLE_NAME_COLLECTION = "collection" ; //收藏表
   private static final String TABLE_NAME_ACCOUNT = "account" ; //账户表
   private static final String TABLE_NAME_PROCUREMENT = "procurement" ; //采购表
   private static final String TABLE_NAME_AFTER_SALES = "after_sales" ; //售后表：支付和退款
   private static final String TABLE_NAME_DAILY_SALES = "daily_sales" ; //日销售额（包括用户ID ， 时间 ， 金额   无主键）
   private static final String TABLE_NAME_MONTHLY_SALES = "monthly_sales" ; //月销售额（包括用户Id , 时间 ， 金额  无主键）
   private static final String TABLE_NAME_TOTAL_SALES = "total_sales" ; //总销售额（包括用户Id：主键 ， 时间 ， 金额 ）

   public DBHelper(Context context) {
      super(context, DATABASE_NAME, null, DATABASE_VERSION);
   }
   private static final String CREATE_TABLE_USER = "CREATE TABLE " + TABLE_NAME_USER + " (" +
           "user_id text primary key autoincrement," +
           "user_name text," +
           "user_password text," +
           "user_pay_password text ," +
           "user_address  text )" ;
   //数据库创建时被调用
   @Override
   public void onCreate(SQLiteDatabase db) {
      db.execSQL(CREATE_TABLE_USER);
//      initTable(db);
//      initData(db);
   }

   //数据库升级
   @Override
   public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//      if (oldVersion < 2) { // 数据库 1 -> 2 升级
//         // 创建设置表
//         db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME_USER);
//         //db.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_NAME_USER + " (keycolumn text primary key,valuecolumn text)");
//         onCreate(db);
//         Log.d(TAG, db.toString());
//      }
      db.execSQL("DROP TABLE IF EXISTS TABLE_NAME_USER");
      onCreate(db);

   }



   /**
    * 初始化数据表
    *
    * @param db 数据库
    */
   private void initTable(SQLiteDatabase db) {
      // 创建商品表
//      db.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_NAME_GOODS + " (" +
//              "goods_id text primary key autoincrement," +
//              "goods_name text," +
//              "goods_price double," +
//              "goods_unit text," +
//              "goods_browse integer," +
//              "goods_description text," +
//              "user_id text " +
//              ")");
      // 创建用户表
      db.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_NAME_USER + " (" +
              "user_id integer primary key autoincrement," +
              "user_name text," +
              "user_password text," +
              "uesr_pay_password text ," +
              "user_address  text )");
//      // 创建订单表
//      db.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_NAME_ORDER + " (" +
//              "order_id integer primary key autoincrement," +
//              "order_name text," +
//              "order_number  bigint," +
//              "user_id  integer," +
//              "FOREIGN KEY (user_id) REFERENCES user(_id))");
//      // 创建图片表
//      db.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_NAME_PHOTO + " (" +
//              "photo_id integer primary key autoincrement," +
//              "goods_id  integer," +
//              "image BLOB," +
//              "FOREIGN KEY (goods_id) REFERENCES goods(_id))");

   }

   /**
    * 初始化数据库内部模拟数据(仅在第一次安装时执行)
    *
    * @param db 数据库
    */
   private void initData(SQLiteDatabase db) {
//      db.execSQL("insert into " + TABLE_NAME_GOODS +
//              " (goods_name , goods_price, goods_unit, goods_browse, goods_description) " +
//              "values" +
//              "('四川峨眉山黄瓜', 1.23, '斤', '123', '来自四川雪山之巅的黄瓜。')");
//      db.execSQL("insert into " + TABLE_NAME_GOODS +
//              " (goods_name , goods_price, goods_unit, goods_browse, goods_description) " +
//              "values" +
//              "('东北大米', 3.33, '斤', '444', '来自东北的大米，煮饭特别香。')");
//      db.execSQL("insert into " + TABLE_NAME_GOODS +
//              " (goods_name , goods_price, goods_unit, goods_browse, goods_description) " +
//              "values" +
//              "('海南香蕉', 9.99, '斤', '213', '海南香蕉，特别好吃')");
//      db.execSQL("insert into " + TABLE_NAME_GOODS +
//              " (goods_name , goods_price, goods_unit, goods_browse, goods_description) " +
//              "values" +
//              "('新疆又香又甜的哈密瓜', 5.99, '斤', '333', '又大又甜，特别好吃')");
//      db.execSQL("insert into " + TABLE_NAME_GOODS +
//              " (goods_name , goods_price, goods_unit, goods_browse, goods_description) " +
//              "values" +
//              "('江苏黄瓜', 1.12, '斤', '223', '来自江苏的黄瓜。')");

      db.execSQL("insert into " + TABLE_NAME_USER +
              " (user_id ,user_name , user_password) " +
              "values" +
              "('10000000001' , '张三', '123456')");
      db.execSQL("insert into " + TABLE_NAME_USER +
              " (user_id ,user_name , user_password) " +
              "values" +
              "('10000000002' , '2', '234567')");
      db.execSQL("insert into " + TABLE_NAME_USER +
              " (user_id , user_name , user_password) " +
              "values" +
              "('10000000003' , '3', '345678')");
   }
}
